package com.logicwin.RobotTravel.Screens;

import java.util.ArrayList;

import com.logicwin.RobotTravel.Services.Impl.ServiceManager;

import com.logicwin.RobotTravel.Services.IScreenService;

import com.logicwin.RobotTravel.CustomDialog;
import com.logicwin.RobotTravel.Main;
import com.logicwin.RobotTravel.R;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ScreenHome extends Screen{
	
	private GridView gridView;
	private ArrayList<ScreenHomeItem> items;	
	private static int itemExitPosition = 3;
	private final IScreenService screenService;
	private ScreenHomeAdapter adapter;

	public ScreenHome() {
		super(SCREEN_TYPE.HOME_T, ScreenHome.class.getCanonicalName());
		screenService = ServiceManager.getScreenService();
	}	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_home);		
		
		this.items = new ArrayList<ScreenHomeItem>();		
		// always visible
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.navigate), "自动语音导游", ScreenNavigate.class));
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.video), "景点点播", ScreenIntro.class));
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.forecast), "天气预报", ScreenForecast.class));			
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.exit_48), "退出",null));
/*		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.options_48), "Options",null));*/
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.about_48), "关于",ScreenAbout.class));	

//		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
//				getResources(), R.drawable.search), "search", ScreenSearch.class));
//
//		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
//				getResources(), R.drawable.bar), "bar", ScreenBar.class));
//		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
//				getResources(), R.drawable.ktv), "ktv", ScreenKtv.class));
//		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
//				getResources(), R.drawable.restaurant), "restaurant", ScreenRestaurant.class));
//		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
//				getResources(), R.drawable.hotel), "hotel", ScreenHotel.class));
	
		// gridView
		this.adapter = new ScreenHomeAdapter(this.items);
		this.gridView = (GridView) this.findViewById(R.id.screen_home_gridview);
		this.gridView.setAdapter(this.adapter);	
		gridView_setOnItemClickListener();
	}
	private class ScreenHomeItem {
		private final Bitmap icon;
		private final String text;
		private final Class<? extends Screen> screenCls;
		
		private ScreenHomeItem(Bitmap icon, String text, Class<? extends Screen> screenCls) {
			this.icon = icon;
			this.text = text;
			this.screenCls = screenCls;
		}
	}
	private class ScreenHomeAdapter extends BaseAdapter {
		private ArrayList<ScreenHomeItem> items;
		
		private ScreenHomeAdapter(ArrayList<ScreenHomeItem> items) {
			this.items = items;
		}
		public int getCount() {
			return this.items.size();
		}
		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			ScreenHomeItem item;

			if (view == null) {
				view = getLayoutInflater().inflate(R.layout.screen_home_item, null);
			}
			if ((item = this.items.get(position)) == null) {
				return view;
			}

			ImageView iv = (ImageView) view.findViewById(R.id.screen_home_item_icon);
			// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			TextView tv = (TextView) view.findViewById(R.id.screen_home_item_text);
				
			tv.setText(item.text);
			iv.setImageBitmap(item.icon);
			return view;
		}
	}
	
	private void gridView_setOnItemClickListener() {
		this.gridView.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ScreenHomeItem item = ScreenHome.this.items.get(position);
				if(item != null){
					if(position == ScreenHome.itemExitPosition){
						CustomDialog.show(
								ScreenHome.this,
								R.drawable.exit_48,
								null,
								"是否需要退出?",
								"确定",
								new DialogInterface.OnClickListener() {									 
									public void onClick(DialogInterface dialog, int which) {
										Main.getMainActivity().exit();
									}
								}, "取消",
								new DialogInterface.OnClickListener() {									 
									public void onClick(DialogInterface dialog, int which) {
										dialog.cancel();
									}
								});
					}
					else{
						ScreenHome.this.screenService.show(item.screenCls, item.screenCls.getCanonicalName());
					}
				}
			}
		});
	}	
	
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
		 
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		
	}	
	protected void onDestroy() { 
        super.onDestroy();
	}	
}
